package cn.felord.debezium.service;

import cn.felord.debezium.dao.ScaleEquipmentMamageMapper;
import cn.felord.debezium.entity.ScaleEquipmentMamage;
import cn.felord.debezium.task.R;
import cn.felord.debezium.utils.AuToSend;
import cn.felord.debezium.utils.CRC16Checksum;
import cn.felord.debezium.utils.String2AcsII16;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class ScaleEquipmentMamageService {
    @Autowired
    ScaleEquipmentMamageMapper scaleEquipmentMamageMapper;
    @Autowired
    AuToSend auToSend;
    public R push_update(Long id) throws IOException {
        ScaleEquipmentMamage scaleEquipmentMamage1 = scaleEquipmentMamageMapper.selectScaleEquipmentMamageById(id);
        String equName = scaleEquipmentMamage1.getEquName();
        ScaleEquipmentMamage scaleEquipmentMamage = new ScaleEquipmentMamage();
        scaleEquipmentMamage.setEquName(equName);
        List<ScaleEquipmentMamage> scaleEquipmentMamages = scaleEquipmentMamageMapper.selectScaleEquipmentMamageList(scaleEquipmentMamage);
        StringBuffer lhs = new StringBuffer();
        for (ScaleEquipmentMamage equipmentMamage : scaleEquipmentMamages) {
            String lh = String2AcsII16.string2hex(equipmentMamage.getLh().trim(),16);
            lhs.append(lh);//两位hex值
        }
        if(scaleEquipmentMamages.size() < 6){
            int bw = 6 - scaleEquipmentMamages.size();
            for (int i = 0; i < bw; i++) {
                lhs.append("0000000000000000");//空炉号补16个0
            }
        }
        String EQUIPMENT_CODE = String2AcsII16.string2hex("KDTC") + scaleEquipmentMamage1.getEquNum();
        int jcq = (EQUIPMENT_CODE + lhs).length() /4;
        int zjs = (EQUIPMENT_CODE + lhs).length() /2;
        String jcq16 = String2AcsII16.int10to16_4(jcq);
        String zjs16 = String2AcsII16.int10to16_4(zjs);
        String crc16ModBus = CRC16Checksum.getCrc16ModBus("01100200" + jcq16 + zjs16 + EQUIPMENT_CODE + lhs);
        String endSend = "FE"+"01100200" + jcq16 + zjs16 + EQUIPMENT_CODE + lhs + crc16ModBus + "FF";
        auToSend.sendEquip(endSend,equName);
        return R.success(equName + "发送成功:" + endSend);
    }
    public R push_update(String equipCode) throws IOException {
        ScaleEquipmentMamage scaleEquipmentMamage = new ScaleEquipmentMamage();
        scaleEquipmentMamage.setEquName(equipCode);
        List<ScaleEquipmentMamage> scaleEquipmentMamages = scaleEquipmentMamageMapper.selectScaleEquipmentMamageList(scaleEquipmentMamage);
        if(null == scaleEquipmentMamages || scaleEquipmentMamages.size() ==0){
            return R.failure("设备编码对应的设备不存在");
        }
        String equipType = "KDHC";
        String equipNum = "00000000";
        StringBuffer lhs = new StringBuffer();
        for (ScaleEquipmentMamage equipmentMamage : scaleEquipmentMamages) {
            String lh = String2AcsII16.string2hex(equipmentMamage.getLh().trim(),16);
            lhs.append(lh);//两位hex值
            equipNum = equipmentMamage.getEquNum();
        }
        if(scaleEquipmentMamages.size() < 6){
            int bw = 6 - scaleEquipmentMamages.size();
            for (int i = 0; i < bw; i++) {
                lhs.append("0000000000000000");//空炉号补16个0
            }
        }
        String EQUIPMENT_CODE = String2AcsII16.string2hex(equipType) + equipNum;
        int jcq = (EQUIPMENT_CODE + lhs).length() /4;
        int zjs = (EQUIPMENT_CODE + lhs).length() /2;
        String jcq16 = String2AcsII16.int10to16_4(jcq);
        String zjs16 = String2AcsII16.int10to16_4(zjs);
        String crc16ModBus = CRC16Checksum.getCrc16ModBus("01100200" + jcq16 + zjs16 + EQUIPMENT_CODE + lhs);
        String endSend = "FE"+"01100200" + jcq16 + zjs16 + EQUIPMENT_CODE + lhs + crc16ModBus + "FF";
        return auToSend.sendEquip(endSend,equipCode);
    }
}
